import { ActionResponseNormal } from "../axdr/ActionResponseNormal";
import { FrameData } from "../bean/FrameData";
import { DataUtils } from "../DataUtils";
import { resultCode } from "../tools/DataTool";
import { ActionResponseNormalList } from "../axdr/ActionResponseNormalList";
import { ActionThenGetResponseNormalList } from "../axdr/ActionThenGetResponseNormalList";
import { parseData } from "./ParseTools";


export function parseActionResponseNormal(actionResponseNormal: ActionResponseNormal): String {
    let result = {
        dar: {},
        data_option: {
            data: {}
        }
    }
    let frameData = new FrameData();
    actionResponseNormal.result.dar.encodeAndSave(1000);
    result.dar = DataUtils.bytesToHexString(actionResponseNormal.result.dar.dataCode, 0, actionResponseNormal.result.dar.dataCode.length);
    if (actionResponseNormal.result.retData.isUsed) {
        result.data_option.data = parseData(actionResponseNormal.result.retData.value);
    } else {
        result.data_option.data = {};
    }
    frameData.code = resultCode.success;
    frameData.data = result;
    return JSON.stringify(frameData);
}

export function parseActionResponseNormalList(actionResponseList: ActionResponseNormalList): String {
    let action_response_list = {
        seq_of_result: {}
    }
    let resultArray = new Array<any>();
    let frameData = new FrameData();
    for (let i = 0; i < actionResponseList.results.size(); i++) {
        let result = {
            dar: {},
            data_option: {
                data: {}
            }
        }
        let retOmdData = actionResponseList.results.get(i);
        retOmdData.dar.encodeAndSave(1000);
        result.dar = DataUtils.bytesToHexString(retOmdData.dar.dataCode, 0, retOmdData.dar.dataCode.length);
        if (retOmdData.retData.isUsed) {
            result.data_option.data = parseData(retOmdData.retData.value);
        } else {
            result.data_option.data = {};
        }
        resultArray.push(result);
    }
    action_response_list.seq_of_result = resultArray;
    frameData.code = resultCode.success
    frameData.data = action_response_list;
    return JSON.stringify(frameData);
}

export function parseActionThenGetResponseList(actionThenGet: ActionThenGetResponseNormalList): String {
    let frameData = new FrameData();
    let action_then_get_list = {
        seq_of_result: {}
    }
    let resultArray = new Array<any>();
    for (let i = 0; i < actionThenGet.reads.size(); i++) {
        let proxy_action_then_get = {
            dar: {},
            data_option: {
                data: {}
            },
            get_result: {}
        }
        let readOmdData = actionThenGet.reads.get(i);
        readOmdData.dar.encodeAndSave(1000);
        proxy_action_then_get.dar = DataUtils.bytesToHexString(readOmdData.dar.dataCode, 0, readOmdData.dar.dataCode.length);
        if (readOmdData.retData.isUsed) {
            proxy_action_then_get.data_option.data = parseData(readOmdData.retData.value);
        } else {
            proxy_action_then_get.data_option.data = {}
        }
        let get_result = readOmdData.result;
        switch (get_result.choice) {
            case 0:
                let dar = {
                    dar: {}
                }
                get_result.dar.encodeAndSave(1000);
                dar.dar = DataUtils.bytesToHexString(get_result.dar.dataCode, 0, get_result.dar.dataCode.length);
                proxy_action_then_get.get_result = dar;
                break;
            case 1:
                proxy_action_then_get.get_result = parseData(get_result.data);
                break;
        }
        resultArray.push(proxy_action_then_get);
    }
    action_then_get_list.seq_of_result = resultArray;
    frameData.code = resultCode.success;
    frameData.data = action_then_get_list;
    return JSON.stringify(frameData);
}
